<template>
  <div class="execution">
    <basic-container>
      <avue-crud ref="crud"
                 :page.sync="page"
                 :data="tableData"
                 :permission="permissionList"
                 :table-loading="tableLoading"
                 @header-cell-class-name="getTableOption"
                 :option="tableOption"
                 @search-reset="searchReset"
                 @on-load="getList"
                 @search-change="searchChange"
                 @refresh-change="refreshChange"
                 @row-update="handleUpdate"
                 @row-save="handleSave"
                 @row-del="rowDel">
        <template slot="search">
          <el-col :span="8" style="margin-bottom: 30px">
            <label class="el-form-item__label" style="width: 80px;">日期:</label>
            <el-date-picker
              size="small"
              style="width: calc(100% - 80px) !important;"
              v-model="dateRange"
              type="daterange"
              value-format="yyyy-MM-dd"
              :picker-options="pickerOptions"
              start-placeholder="开始日期"
              end-placeholder="结束日期">
            </el-date-picker>
          </el-col>
        </template>
      </avue-crud>
    </basic-container>
  </div>
</template>

<script>
  import { fetchList, getObj, addObj, putObj, delObj, getTotalSummary } from '@/api/crm/daySummary';
  import { tableOption, getTableOption, getTableData } from '@/const/crud/crm/reporting/daySummary';
  import {
    mapGetters
  } from 'vuex'

  export default {
    name: 'salescontract',
    data() {
      let _minTime = null;
      let _maxTime = null;
      return {
        tableData: [],
        page: {
          total: 0, // 总页数
          currentPage: 1, // 当前页数
          pageSize: 20 // 每页显示多少条
        },
        tableLoading: false,
        tableOption: tableOption,
        dateRange:'',
        pickerOptions: {
          onPick(time){
            if (!time.maxDate) {
              let timeRange = 6 * 24 * 60 * 60 * 1000;
              _minTime = time.minDate.getTime() - timeRange;
              _maxTime = time.minDate.getTime() + timeRange ;
            } else {
              _minTime = _maxTime = null;
            }
          },
          disabledDate(time) {
            if(_minTime && _maxTime){
              return time.getTime() < _minTime || time.getTime() > _maxTime;
            }
          }
        },
      }
    },
    computed: {
      ...mapGetters(['permissions']),
      permissionList() {
        return {
          addBtn: this.vaildData(this.permissions.crm_salescontract_add, false),
          delBtn: this.vaildData(this.permissions.crm_salescontract_del, false),
          editBtn: this.vaildData(this.permissions.crm_salescontract_edit, false)
        };
      }
    },
    methods: {
      getTableOption(data) {
        getTableOption(data);
      },
      async getList(page, params) {
        this.tableLoading = true;
        let query = Object.assign({
          current: page.currentPage,
          size: page.pageSize
        }, params, getDate(this.dateRange));
        try {
          let res = await getTotalSummary(query);
          this.tableLoading = false;
          this.tableOption = getTableOption(res.data.data.records);
          this.tableData = getTableData(res.data.data.records);
          this.page.total = res.data.data.total;
        }catch(err) {
          this.$message.error(err);
          this.tableLoading = false;
        }
      },
      searchReset() {
        this.dateRange = '';
      },
      rowDel: function (row, index) {
        var _this = this
        this.$confirm('是否确认删除ID为' + row.id, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function () {
          return delObj(row.id)
        }).then(data => {
          _this.tableData.splice(index, 1)
          _this.$message({
            showClose: true,
            message: '删除成功',
            type: 'success'
          })
          this.getList(this.page)
        }).catch(function (err) {
        })
      },
      /**
       * @title 数据更新
       * @param row 为当前的数据
       * @param index 为当前更新数据的行数
       * @param done 为表单关闭函数
       *
       **/
      handleUpdate: function (row, index, done) {
        putObj(row).then(data => {
          this.tableData.splice(index, 1, Object.assign({}, row))
          this.$message({
            showClose: true,
            message: '修改成功',
            type: 'success'
          })
          done()
          this.getList(this.page)
        })
      },
      /**
       * @title 数据添加
       * @param row 为当前的数据
       * @param done 为表单关闭函数
       *
       **/
      handleSave: function (row, done) {
        addObj(row).then(data => {
          this.tableData.push(Object.assign({}, row))
          this.$message({
            showClose: true,
            message: '添加成功',
            type: 'success'
          })
          done()
          this.getList(this.page)
        })
      },
      /**
       * 搜索回调
       */
      searchChange(form, done) {
                this.getList(this.page, form);
                done();
            },
      /**
       * 刷新回调
       */
      refreshChange(info) {
    this.getList(this.page, this.filterForm(info.searchForm));
}
    }
  }

function getDate(dateRange) {
  if(!Array.isArray(dateRange) || dateRange.length !== 2) return;
  return {
    startDate: dateRange[0],
    endDate: dateRange[1],
  }
}
</script>

<style lang="scss" scoped>
.el-range-editor--small.el-input__inner {
    height: 32px !important;
}
</style>
